use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput};
use hysteria_core::protocol::{TcpRequest, TcpResponse, UdpMessage};
use hysteria_core::crypto::SalamanderObfuscator;

fn tcp_request_benchmark(c: &mut Criterion) {
    let mut group = c.benchmark_group("tcp_request");
    
    group.bench_function("encode", |b| {
        let req = TcpRequest::new("example.com:443".to_string());
        b.iter(|| {
            black_box(req.encode());
        });
    });

    group.bench_function("decode", |b| {
        let req = TcpRequest::new("example.com:443".to_string());
        let encoded = req.encode();
        b.iter(|| {
            black_box(TcpRequest::decode(&encoded).unwrap());
        });
    });

    group.finish();
}

fn salamander_benchmark(c: &mut Criterion) {
    let mut group = c.benchmark_group("salamander");
    
    let obfuscator = SalamanderObfuscator::new("test_password");
    let data = vec![0u8; 1024];
    
    group.throughput(Throughput::Bytes(1024));
    
    group.bench_function("obfuscate_1kb", |b| {
        b.iter(|| {
            black_box(obfuscator.obfuscate(&data));
        });
    });

    let obfuscated = obfuscator.obfuscate(&data);
    group.bench_function("deobfuscate_1kb", |b| {
        b.iter(|| {
            black_box(obfuscator.deobfuscate(&obfuscated).unwrap());
        });
    });

    group.finish();
}

fn udp_message_benchmark(c: &mut Criterion) {
    let mut group = c.benchmark_group("udp_message");
    
    group.bench_function("encode", |b| {
        let msg = UdpMessage::new(12345, "8.8.8.8:53".to_string(), vec![1, 2, 3, 4]);
        b.iter(|| {
            black_box(msg.encode());
        });
    });

    group.bench_function("decode", |b| {
        let msg = UdpMessage::new(12345, "8.8.8.8:53".to_string(), vec![1, 2, 3, 4]);
        let encoded = msg.encode();
        b.iter(|| {
            black_box(UdpMessage::decode(&encoded).unwrap());
        });
    });

    group.finish();
}

criterion_group!(
    benches,
    tcp_request_benchmark,
    salamander_benchmark,
    udp_message_benchmark
);
criterion_main!(benches);
